const axios = require("./epointajax.js");
const user = require("./user.js");
const wd = require("./window.js");
const cache = require("./lib/cache.js");

const {
  remote,
  ipcRenderer
} = require('electron');
const {
  globalShortcut
} = remote;



const shotcut = "shotcut"; //快捷键设置

//先移除所有，再遍历注册，这个obj是渲染对象，需要一直存在。
function initUserShortcut(obj) {
  var config = cache.get(shotcut);
  unregisterAll();
  if (config != undefined) {
    console.log(config);

    for (var name in config) {
      saveShortcutCmd(name, obj, config[name]);
    }

  }
}


function isRegister(shotcmd) {
  return globalShortcut.isRegistered(shotcmd);
}


function getCmd(...shots) {
  var shotcmd = "";
  for (let i = 0; i < shots.length; i++) {
    if (i == shots.length - 1) {
      shotcmd += shots[i];
    } else {
      shotcmd += shots[i] + "+";
    }
  }
  return shotcmd;
}

function saveShortcutCmd(name, obj, shotcmd) {
  let result = false;
  if (isRegister(shotcmd)) {
    alert("快捷键" + shotcmd + "已经被占用！");
    return;
  }

  if (name == "showWindow") {
    //显示
    result = saveshowWindow(name, obj, shotcmd);
  } else if (name == "minWindow") {
    //隐藏
    result = saveminWindow(name, obj, shotcmd);
  } else if (name == "showAndmin") {
    //老板键，显示隐藏切换
    result = saveshowAndmin(name, obj, shotcmd);
  }
  if (result) {
    //保存到缓存
    addShortcut(name, shotcmd);
  }
  return result;
}


//本机设置成功后，保存到服务器
function saveShortcut(name, obj, ...shots) {
  removeShortcut(name);
  let shotcmd = getCmd(...shots);
  let result = saveShortcutCmd(name, obj, shotcmd);
  if (result) {
    alert("注册快捷键" + shotcmd + "成功");
  } else {
    alert("注册快捷键" + shotcmd + "失败，换一个试试");
  }

}

function getShortcut(name) {
  return cache.get(shotcut) != undefined ? cache.get(shotcut)[name] : undefined;
}


//用户设置
function addShortcut(name, value) {
  var config = cache.get(shotcut);
  if (config == undefined) {
    config = {};
  }
  config[name] = value;
  cache.set(shotcut, config);
}

function removeShortcut(name) {
  let oldcmd = getShortcut(name);
  if (oldcmd != undefined) {
    globalShortcut.unregister(oldcmd);
  }
}

//移除所有快键键和缓存
function unregisterAll() {
  globalShortcut.unregisterAll();
  cache.remove(shotcut);
}

//恢复窗口
function saveshowWindow(name, obj, shotcmd) {
  console.log("保存快捷键" + name + shotcmd);
  const ret = globalShortcut.register(shotcmd, () => {
    wd.showWindow(obj);

  })
  if (ret) {
    console.log("快捷键注册成功");
  }
  return ret;
}


//缩小到任务栏
function saveminWindow(name, obj, shotcmd) {
  console.log("保存快捷键" + name + shotcmd);
  const ret = globalShortcut.register(shotcmd, () => {
    wd.minWindow(obj);
  })

  if (ret) {
    console.log("快捷键注册成功")
  }
  return ret;
}


//缩小到任务栏
function saveshowAndmin(name, obj, shotcmd) {
  console.log("保存快捷键" + name + shotcmd);
  const ret = globalShortcut.register(shotcmd, () => {
    wd.showAndmin(obj);
  })

  if (ret) {
    console.log("快捷键注册成功")
  }
  return ret;
}



module.exports.isRegister = isRegister;
module.exports.initUserShortcut = initUserShortcut;
module.exports.saveShortcut = saveShortcut;
module.exports.unregisterAll = unregisterAll;
module.exports.getShortcut = getShortcut;
